package com.wlr.study.leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author lerong.wang
 * @version 1.0.0
 * @description
 * @date 2025/4/18 10:13
 */
public class LeetCode47 {

    /**
     * 给定一个可包含重复数字的序列 nums ，按任意顺序 返回所有不重复的全排列。
     *
     * @param nums
     * @return
     */
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> inter = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            inter.add(nums[i]);
        }
        int n = nums.length;
        dfs(n, inter, result, 0);
        return result;
    }

    private void dfs(int n, List<Integer> inter, List<List<Integer>> result, int first) {
        if (first == n) {
            result.add(new ArrayList<>(inter));
            return;
        }
        for (int i = first; i < n; i++) {
            Collections.swap(inter, first, i);
            dfs(n, inter, result, first + 1);
            Collections.swap(inter, first, i);
        }

    }

    public static void main(String[] args) {
        LeetCode47 leetCode47 = new LeetCode47();
        System.out.println(leetCode47.permuteUnique(new int[]{1, 1, 2}));
    }

}